/*
 * @Author: 消息逻辑
 */

'use strict'

YX.fn.message = function() {
		this.$sendBtn = $('#sendBtn')
		this.$messageText = $('#messageText')
		this.$chooseFileBtn = $('#chooseFileBtn')
		this.$fileInput = $('#uploadFile')

		this.$sendBtn.on('click', this.sendTextMessage.bind(this))
		this.$messageText.on('keydown', this.inputMessage.bind(this))
		this.$chooseFileBtn.on('click', 'a', this.chooseFile.bind(this))
		this.$fileInput.on('change', this.uploadFile.bind(this))
			//消息重发
		this.$chatContent.delegate('.j-resend', 'click', this.doResend.bind(this))
			//语音播发
		this.$chatContent.delegate('.j-mbox', 'click', this.playAudio)

		this.ame = ""
			//聊天面板右键菜单
			/*		$.contextMenu({
							selector: '.item-me .j-msg',
							callback: function(key, options) {
								if(key === 'delete') {
									var id = options.$trigger.parent().data('id')
									var msg = this.cache.findMsg(this.crtSession, id)
									if(!msg || options.$trigger.hasClass('j-msg')) {}
									options.$trigger.removeClass('j-msg')
									this.nim.deleteMsg({
										msg: msg,
										done: function(err) {
											options.$trigger.addClass('j-msg')
											if(err) {
												if(err.code === 508) {
													alert('发送时间超过2分钟的消息，不能被撤回')
												} else {
													alert(err.message || '操作失败')
												}
											} else {
												this.backoutMsg(id)
											}
										}.bind(this)
									})
								}
								
							}.bind(this),
							items: {
								"delete": {
									name: "撤回",
									icon: "delete"
								}
							}
						})*/

		$.contextMenu({
			selector: '.item-me .j-msg',
			callback: function(key, options) {
				if(key === 'delete') {
					var id = options.$trigger.parent().data('id')
					var msg = this.cache.findMsg(this.crtSession, id)
					if(!msg || options.$trigger.hasClass('j-msg')) {}
					options.$trigger.removeClass('j-msg')
					this.nim.deleteMsg({
						msg: msg,
						done: function(err) {
							options.$trigger.addClass('j-msg')
							if(err) {
								if(err.code === 508) {
									alert('发送时间超过2分钟的消息，不能被撤回')
								} else {
									alert(err.message || '操作失败')
								}
							} else {
								this.backoutMsg(id)
							}
						}.bind(this)
					})
				}

				if(key === "edit") {
					//更改加密对象
					var id = options.$trigger.parent().data('id')
					var msg = this.cache.findMsg(this.crtSession, id)

					if(msg.custom) {
						var custom = JSON.parse(msg.custom)
						if(custom.receivers) {

							this.Editencryption(id)

						}

					}
				}

			}.bind(this),
			items: {
				"delete": {
					name: "撤回",
					icon: "delete"
				},
				"edit": {
					name: "修改加密对象",
					icon: "edit"
				}
			}
		})

		//表情贴图模块
		this.initEmoji()

	}
	/**
	 * 处理收到的消息 
	 * @param  {Object} msg 
	 * @return 
	 */
YX.fn.doMsg = function(msg) {
	console.log("msg")
		var that = this,
			who = msg.to === userUID ? msg.from : msg.to,
			updateContentUI = function() {
				//如果当前消息对象的会话面板打开
				if(that.crtSessionAccount === who) {
					console.log("--")
					that.sendMsgRead(who, msg.scene)
					var msgHtml = appUI.updateChatContentUI(msg, that.cache)
					that.$chatContent.find('.no-msg').remove()
					that.$chatContent.append(msgHtml).scrollTop(99999)
				}
			}
			//非群通知消息处理
		if(/text|image|file|audio|video|geo|custom|tip/i.test(msg.type)) {
			this.cache.addMsgs(msg)
			var account = (msg.scene === "p2p" ? who : msg.from)
				//用户信息本地没有缓存，需存储
			if(!this.cache.getUserById(account)) {
				this.mysdk.getUser(account, function(err, data) {
					if(!err) {
						that.cache.updatePersonlist(data)
						updateContentUI()
					}
				})
			} else {
				this.buildSessions()
					// this.buildTeams()
				updateContentUI()
			}
		} else {
			// 群消息处理
			console.log("收到群消息")
			this.messageHandler(msg, updateContentUI)
		}
	}
	/*****************************************************************
	 * emoji模块
	 ****************************************************************/
YX.fn.initEmoji = function() {
		this.$showEmoji = $('#showEmoji')
		this.$showEmoji.on('click', this.showEmoji.bind(this))
		var that = this,
			emojiConfig = {
				'emojiList': emojiList, //普通表情
				'pinupList': pinupList, //贴图
				'width': 500,
				'height': 300,
				'imgpath': './images/',
				'callback': function(result) {
					that.cbShowEmoji(result)
				}
			}
		this.$emNode = new CEmojiEngine($('#emojiTag')[0], emojiConfig)

	}
	/**
	 * 选择表情回调
	 * @param  {objcet} result 点击表情/贴图返回的数据
	 */
YX.fn.cbShowEmoji = function(result) {
	if(!!result) {
		var scene = this.crtSessionType,
			to = this.crtSessionAccount
			// 贴图，发送自定义消息体
		if(result.type === "pinup") {
			var index = Number(result.emoji) + 1
			var content = {
				type: 3,
				data: {
					catalog: result.category,
					chartlet: result.category + '0' + (index >= 10 ? index : '0' + index)
				}
			}
			this.mysdk.sendCustomMessage(scene, to, content, this.sendMsgDone.bind(this))
		} else {
			// 表情，内容直接加到输入框
			this.$messageText[0].value = this.$messageText[0].value + result.emoji
		}
	}
}

YX.fn.showEmoji = function() {
		this.$emNode._$show()
	}
	/*************************************************************************
	 * 发送消息逻辑
	 * 
	 ************************************************************************/
YX.fn.uploadFile = function() {
	var that = this,
		scene = this.crtSessionType,
		to = this.crtSessionAccount,
		fileInput = this.$fileInput.get(0)
	if(fileInput.files[0].size == 0) {
		alert("不能传空文件")
		return
	}
	this.mysdk.sendFileMessage(scene, to, fileInput, this.sendMsgDone.bind(this))
}

YX.fn.chooseFile = function() {
	this.$fileInput.click()
}

///////////////改过
YX.fn.sendTextMessage = function() {
	var scene = this.crtSessionType,
		to = this.crtSessionAccount;

	//可见对象
	var receivers = []
	for(var key in chooseUserData.checkedUsers) {
		//只遍历对象自身的属性，而不包含继承于原型链上的属性。  
		if(chooseUserData.checkedUsers.hasOwnProperty(key) === true) {
			receivers.push(key);
		}
	}

	//@的对象
	var account = [];
	//所有的span标签
	var spans = $('#messageText').find("span");
	for(var i = 0; i < spans.length; i++) {
		account.push(spans.eq(i).attr("account"));
	}
	var text = $('#messageText').text()
	if(!!to && !!text) {
		if(text.length > 500) {
			alert('消息长度最大为500字符')
		} else if(text.length === 0) {
			return
		} else {
			this.mysdk.sendTextMessage(scene, to, text, receivers, account, this.sendMsgDone.bind(this))
		}
	}
}

/**
 * 发送消息完毕后的回调
 * @param error：消息发送失败的原因
 * @param msg：消息主体，类型分为文本、文件、图片、地理位置、语音、视频、自定义消息，通知等
 */
/*YX.fn.sendMsgDone = function (error, msg) {
    this.cache.addMsgs(msg)
    this.$messageText.val('')
    this.$chatContent.find('.no-msg').remove()
    var msgHtml = appUI.updateChatContentUI(msg,this.cache)
    this.$chatContent.append(msgHtml).scrollTop(99999)
    $('#uploadForm').get(0).reset()
}*/

//改
YX.fn.sendMsgDone = function(error, msg) {
	this.cache.addMsgs(msg)
	this.$messageText.html("<p>&nbsp;<p>")
	this.$chatContent.find('.no-msg').remove()
	var msgHtml = appUI.updateChatContentUI(msg, this.cache)
	this.$chatContent.find(".mCSB_container").append(msgHtml).scrollTop(99999)
	$('#uploadForm').get(0).reset()

	this.encryptionNo()
}

//修改加密范围消息发送成功后的回调
YX.fn.resetVisibleMsgDone = function(error, msg) {
	this.cache.addMsgs(msg)
	this.$messageText.html("<p>&nbsp;<p>")
	this.$chatContent.find('.no-msg').remove()
		//发送自定义提醒消息
	this.nim.sendTipMsg({
		isLocal: true,
		scene: msg.scene,
		to: to,
		tip: (userUID === msg.from ? '你' : getNick(msg.from)) + '修改了加密范围',
		time: msg.time,
		done: function(err, data) {
			if(!err) {
				this.cache.de(session, id, data)
				if(this.crtSession === session) {
					var msgHtml = appUI.buildChatContentUI(this.crtSession, this.cache)
					this.$chatContent.html(msgHtml).scrollTop(99999)
				}
			} else {
				alert('操作失败')
			}
		}.bind(this)
	})

	var msgHtml = appUI.updateChatContentUI(msg, this.cache)
	this.$chatContent.append(msgHtml).scrollTop(99999)
	$('#uploadForm').get(0).reset()

	this.encryptionNo()
}

var gbiao;
YX.fn.inputMessage = function(e) {
	var ev = e || window.event;
	var str = this.$messageText.text

		//点击@键
	if((ev.keyCode === 50 || ev.keyCode === 229) && ev.shiftKey) {
		var that = this;
		//查找所有的群成员
		var teamId = that.crtSessionAccount;
		this.mysdk.getTeamMembers(teamId, function(error, obj) {
			var members = obj.members;
			//渲染@列表
			$("#person-ul ul").html("");
			for(var i = 0; i < members.length; i++) {
				var member = members[i],
					account = member.account,
					avatar = getAvatar(that.cache.getUserById(account) ? that.cache.getUserById(account).avatar : ""),
					nick = getNick(account)
					//不能@自己
				if(userUID == account) {
					continue
				}
				$("#person-ul ul").append("<li onclick=\"ZDYalternative('" + account + "','" + nick + "')\"><span>" + nick + "</span></li>");
				var selection = window.getSelection() || document.getSelection();
				gbiao = selection.getRangeAt(0);
				var altNode = document.createElement('span');
				altNode.setAttribute("altSpan", "alt");
				altNode.setAttribute("id", "altSpan");

				var aTextNode = document.createTextNode("\u00A0")

				altNode.appendChild(aTextNode)

				//altNode.innerHTML = "@";
				gbiao.insertNode(altNode);
				selection.collapse(altNode, 1);
				gbiao.collapse();
				$("#person-ul").css("display", "block");
				var menuHeight = 40
				$("#person-ul").css("top", altNode.offsetTop + menuHeight - $("#person-ul").height() - $("#messageText")[0].scrollTop)
				$("#person-ul").css("left", altNode.offsetLeft + 20 - $("#messageText")[0].scrollLeft)
			}
		});
		//return false;
	} else {
		if($("span[altSpan]").length > 0) {
			var selection = window.getSelection() || document.getSelection();
			selection.removeAllRanges()
			selection.addRange(gbiao)
			var range = selection.getRangeAt(0)

			//获取光标位置
			//var rangeStartOffset = range.startOffset
			//创建空格
			var nbspNode = document.createTextNode("\u00A0")
				//selection.collapseToEnd()
				//将<span>@</span>变成纯文本@
			$("span[id=altSpan]").replaceWith(nbspNode)
			selection.collapse(nbspNode, 1);
			//range.insertNode(nbspNode)
		}

		//将焦点移动向后一个字符
		$("#person-ul").css("display", "none");

	}

	if($.trim(str).length > 0) {
		if(ev.keyCode === 13 && ev.ctrlKey) {
			this.$messageText.val(this.$messageText.val() + '\r\n')
		} else if(ev.keyCode === 13 && !ev.ctrlKey) {
			this.sendTextMessage()
		}
	}

	if(this.$messageText.html() == "") {
		this.$messageText.html("<p>&nbsp;</p>");
	}

}

//zdy方法，选择@对象
function ZDYalternative(account, name) {
	$("span[altSpan]").remove()
	var selection = window.getSelection();
	selection.removeAllRanges()
	selection.addRange(gbiao)
	var range = selection.getRangeAt(0)

	//获取光标位置
	var rangeStartOffset = range.startOffset
		//创建空格
	var nbspNode = document.createTextNode("\u00A0")
	range.insertNode(nbspNode)

	//创建@span标签
	var spanNode = document.createElement('span')
	spanNode.setAttribute("account", account)
		/*spanNode.innerHTML = "@" + name*/
	spanNode.innerHTML = name

	range.insertNode(spanNode)
	selection.collapse(nbspNode, 1)

	$("#person-ul").css("display", "none")

}

// 重发
YX.fn.doResend = function(evt) {
		var $node
		if(evt.target.tagName.toLowerCase() === 'span') {
			$node = $(evt.target)
		} else {
			$node = $(evt.target.parentNode)
		}
		var sessionId = $node.data("session")
		var idClient = $node.data("id")
		var msg = this.cache.findMsg(sessionId, idClient)
		this.mysdk.resendMsg(msg, function(err, data) {
			if(err) {
				alert(err.message || '发送失败')
			} else {
				this.cache.setMsg(sessionId, idClient, data)
				var msgHtml = appUI.buildChatContentUI(sessionId, this.cache)
				this.$chatContent.html(msgHtml).scrollTop(99999)
				$('#uploadForm').get(0).reset()
			}
		}.bind(this))
	}
	/************************************************************
	 * 获取当前会话消息
	 * @return {void}
	 *************************************************************/
YX.fn.getHistoryMsgs = function(scene, account) {
		var id = scene + "-" + account;
		var sessions = this.cache.findSession(id)
		var msgs = this.cache.getMsgs(id)

		//@me的最前的一条msg的idClient
		var ame = $("li[data-account='" + account + "']").attr("a-me")
		this.ame = ame
			//标记已读回执
		this.sendMsgRead(account, scene)
		if(!!sessions) {
			if(sessions.unread >= msgs.length) {
				var end = (msgs.length > 0) ? msgs[0].time : false
				this.mysdk.getLocalMsgs(id, end, this.getLocalMsgsDone.bind(this))
				return
			}
		}
		this.doChatUI(id, ame)
	}
	//拿到历史消息后聊天面板UI呈现
YX.fn.doChatUI = function(id, ame) {
	//销毁滚动条
	$("#chatContent").mCustomScrollbar("destroy")

	var temp = appUI.buildChatContentUI(id, this.cache)
	this.$chatContent.html(temp)


	//重新初始化
	$("#chatContent").mCustomScrollbar({
		theme: "light-3"
	})

	//有人@我
	if(this.ame != "") {
		let ameMsg = this.cache.findMsg(id, this.ame)
		let user = this.cache.getUserById(ameMsg.from)
		let avatar = user.avatar
		$(".a-me-img img").attr("src", getAvatar(avatar))
		$(".a-me-from").html(getNick(user.account))
		console.log(getNick(user.account) + "---" + user.account)
		$(".a-me-contont").html(ameMsg.text)
		$("#zdy-a-me-tips").data("idclient", this.ame)
		$("#zdy-a-me-tips").show()
	}
	else{
		$("#zdy-a-me-tips").hide()
	}

	//定位
	$("#chatContent").mCustomScrollbar('scrollTo','bottom')
	//this.$chatContent.scrollTop(9999)
		//已读回执UI处理
	this.markMsgRead(id)
}

YX.fn.getLocalMsgsDone = function(err, data) {
	if(!err) {
		this.cache.addMsgsByReverse(data.msgs)
		var id = data.sessionId
		var array = getAllAccount(data.msgs)
		var that = this
		this.checkUserInfo(array, function() {
			that.doChatUI(id)
		})
	} else {
		alert("获取历史消息失败")
	}
}

//检查用户信息有木有本地缓存 没的话就去拿拿好后在执行回调
YX.fn.checkUserInfo = function(array, callback) {
		var arr = []
		var that = this
		for(var i = array.length - 1; i >= 0; i--) {
			if(!this.cache.getUserById(array[i])) {
				arr.push(array[i])
			}
		}
		if(arr.length > 0) {
			this.mysdk.getUsers(arr, function(error, data) {
				if(!error) {
					that.cache.setPersonlist(data)
					callback()
				} else {
					alert("获取用户信息失败")
				}
			})
		} else {
			callback()
		}
	}
	//发送已读回执
YX.fn.sendMsgRead = function(account, scene) {
		if(scene === "p2p") {
			var id = scene + "-" + account
			var sessions = this.cache.findSession(id)
			this.mysdk.sendMsgReceipt(sessions.lastMsg, function(err, data) {
				if(err) {
					console.log(err)
				}
			})
		}
	}
	//UI上标记消息已读
YX.fn.markMsgRead = function(id) {
		if(!id || this.crtSession !== id) {
			return
		}
		var msgs = this.cache.getMsgs(id)
		for(var i = msgs.length - 1; i >= 0; i--) {
			var message = msgs[i]
				// 目前不支持群已读回执
			if(message.scene === "team") {
				return
			}
			if(window.nim.isMsgRemoteRead(message)) {
				$(".item.item-me.read").removeClass("read")
				$("#" + message.idClient).addClass("read")
				break
			}
		}
	}
	//撤回消息
YX.fn.backoutMsg = function(id, data) {
	var msg = data ? data.msg : this.cache.findMsg(this.crtSession, id)
	var to = msg.target
	var session = msg.sessionId
	this.nim.sendTipMsg({
		isLocal: true,
		scene: msg.scene,
		to: to,
		tip: (userUID === msg.from ? '你' : getNick(msg.from)) + '撤回了一条消息',
		time: msg.time,
		done: function(err, data) {
			if(!err) {
				this.cache.backoutMsg(session, id, data)
				if(this.crtSession === session) {
					var msgHtml = appUI.buildChatContentUI(this.crtSession, this.cache)
					this.$chatContent.html(msgHtml).scrollTop(99999)
				}
			} else {
				alert('操作失败')
			}
		}.bind(this)
	})
}

//更改加密对象发送自定义系统消息
YX.fn.editMsg = function(id, data) {
	var msg = data ? data.msg : this.cache.findMsg(this.crtSession, id)
	var to = msg.target
	var session = msg.sessionId

	//可见对象
	var receivers = []
	for(var key in chooseUserData.checkedUsers) {
		//只遍历对象自身的属性，而不包含继承于原型链上的属性。  
		if(chooseUserData.checkedUsers.hasOwnProperty(key) === true) {
			receivers.push(key);
		}
	}

	//var msg = this.cache.findMsg(this.crtSession,chooseUserData.msgid)

	var scene = this.crtSessionType
	var text = msg.text
		//@的对象
	var account = []
	if(msg.apns) {
		if(msg.apns.accounts) {
			account = msg.apns.accounts
		}
	}

	//删除本地msg
	this.cache.deleteMsg(session, id)

	//消息重发
	this.mysdk.sendTextMessage(scene, to, text, receivers, account, this.resetVisibleMsgDone.bind(this))

}